﻿' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports DotNetNuke
Imports DotNetNuke.Entities.Modules.Actions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports MVPcsIframe.Components
Imports DotNetNuke.UI.Utilities

''' <summary>
''' The View class displays the content
''' 
''' Typically your view control would be used to display content or functionality in your module.
''' 
''' View may be the only control you have in your project depending on the complexity of your module
''' 
''' Because the control inherits from MVPcsIframeModuleBase you have access to any custom properties
''' defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
''' 
''' </summary>
Partial Class View
    Inherits MVPcsIframeModuleBase
    Implements IActionable

    Private _currentItemId As Integer = 0
    Private _GDoc As Boolean = False
    Private _MultiURL As Boolean = False
    Private _ifWidth As Integer = 0
    Private _ifHeight As Integer = 0
    Private _Scroll As String = "auto"


    Public Property CurrentItemId() As Integer
        Get
            Return _currentItemId
        End Get
        Set(value As Integer)
            _currentItemId = value
        End Set
    End Property

    ''' <summary>
    ''' Property that Sets Google Doc viewer
    ''' </summary>
    ''' <value></value>
    ''' <returns>GDoc As Boolean</returns>
    ''' <remarks></remarks>
    Public Property GDoc() As Boolean
        Get
            Return _GDoc
        End Get
        Set(value As Boolean)
            _GDoc = value
        End Set
    End Property

    ''' <summary>
    ''' Property that Sets multi Doc viewer list
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property MultiURL() As Boolean
        Get
            Return _MultiURL
        End Get
        Set(value As Boolean)
            _MultiURL = value
        End Set
    End Property


    Public Property ifWidth() As Integer
        Get
            Return _ifWidth
        End Get
        Set(value As Integer)
            _ifWidth = value
        End Set
    End Property


    Public Property ifHeight As Integer
        Get
            Return _ifHeight
        End Get
        Set(value As Integer)
            _ifHeight = value
        End Set
    End Property

    Public Property Scroll As String
        Get
            Return _Scroll
        End Get
        Set(value As String)
            _Scroll = value
        End Set
    End Property


    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Registers the module actions required for interfacing with the portal framework
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions
        Get
            'set the edit and add modes 

            Dim Actions As New ModuleActionCollection
            Dim tc As New ItemController()
            Dim count As Integer = 0
            Load_Setting()
            If (ModuleId > -1) Then
                count = tc.GetItems(ModuleId).Count
            End If

            If (MultiURL = "False") And (count > 0) Then
                Actions.Add(GetNextActionID, Localization.GetString("EditModule", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

            Else
                Actions.Add(GetNextActionID, Localization.GetString("AddItem", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

            End If


            Return Actions
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Page_Load runs when the control is loaded
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try

            If (Not Page.IsPostBack) Then

                Load_Setting()

            End If

            Dim tc As New ItemController()

            If (MultiURL) Then
                dvItleList.Visible = True
                rptItemList.Visible = True
                rptItemList.DataSource = tc.GetItems(ModuleId)
                rptItemList.DataBind()
            Else
                dvItleList.Visible = False
            End If

            If ItemId > 0 Then
                CurrentItemId = ItemId
            Else
                If tc.GetItems(ModuleId).Count > 0 Then
                    CurrentItemId = tc.GetItems(ModuleId).First.ItemId
                End If
            End If

            If CurrentItemId > 0 Then

                Dim t As Item = tc.GetItem(CurrentItemId, ModuleId)
                If t IsNot Nothing Then

                    Load_iFrame(t)

                End If

            End If

        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try

    End Sub

    Private Function KeyValues(ByVal Key As String, ByVal Value As String, ByRef Place As Integer) As String
        Dim _keyValue As String = ""

        If (Not Key = "") And (Not Value = "") Then
            If Place = 1 Then
                _keyValue = "?" + Key + "=" + Value
            ElseIf Place > 1 Then
                _keyValue = "&" + Key + "=" + Value
            End If

        End If

        Return _keyValue
    End Function

    Private Sub Load_Setting()
        CurrentItemId = 0

        If (Settings.Contains("GDoc")) Then
            If (Not Settings("GDoc").ToString() = "") Then
                GDoc = Settings("GDoc").ToString()
            Else
                GDoc = "False"
            End If
        Else
            GDoc = "False"
        End If

        If (Settings.Contains("MultiURL")) Then
            If (Not Settings("MultiURL").ToString = "") Then
                MultiURL = Settings("MultiURL").ToString
            Else
                MultiURL = "False"
            End If
        Else
            MultiURL = "False"
        End If

        If (Settings.Contains("Height")) Then
            If (Not Settings("Height").ToString = "") Then
                Integer.TryParse(Settings("Height").ToString, ifHeight)
            Else
                ifHeight = 0
            End If
        Else
            ifHeight = 0
        End If

        If (Settings.Contains("Width")) Then
            If (Not Settings("Width") = "") Then
                Integer.TryParse(Settings("Width").ToString, ifWidth)
            Else
                ifWidth = 0
            End If
        Else
            ifWidth = 0
        End If

        If (Settings.Contains("Scroll")) Then
            If (Not Settings("Scroll").ToString = "") Then
                Scroll = Settings("Scroll").ToString
            Else
                Scroll = "auto"
            End If
        Else
            Scroll = "auto"
        End If

    End Sub

    Private Sub Load_iFrame(ByVal t As Item)
        Dim height As String
        Dim width As String
        ' InStr(t.ItemURL, )
        If (t.ItemURL.EndsWith(".pdf") _
            OrElse t.ItemURL.EndsWith(".xls") _
            OrElse t.ItemURL.EndsWith(".doc") _
            OrElse t.ItemURL.EndsWith(".ppt") _
            OrElse t.ItemURL.EndsWith(".docx") _
            OrElse t.ItemURL.EndsWith(".xlsx") _
            OrElse t.ItemURL.EndsWith(".pptx")) Then
            'DownLoadLink.Visible = True
            'DownLoadLink.PostBackUrl = t.ItemURL
            DLLink.Attributes("href") = t.ItemURL
            DLLink.Visible = True
        Else
            'DownLoadLink.Visible = False
            DLLink.Visible = False
        End If

        Dim ItemURL As String = t.ItemURL + KeyValues(t.Key1, t.Value1, 1) + KeyValues(t.Key2, t.Value2, 2) + KeyValues(t.Key3, t.Value3, 3)
        Dim GoogleViewer As String = "http://docs.google.com/gview?url=" + ItemURL + "&embedded=true"

        If (GDoc) Then
            Me.myFrame.Attributes("src") = GoogleViewer
        Else
            Me.myFrame.Attributes("src") = ItemURL
        End If

        If (Not ifWidth = Nothing) Or (Not ifWidth = 0) Then
            width = ifWidth.ToString + "px; "
        Else
            width = "100%; "
        End If

        If (Not ifHeight = Nothing) Or (Not ifHeight = 0) Then
            height = ifHeight.ToString + "px; "
        Else
            height = "800px; "
        End If

        Dim style As String = "height: " + height + "width: " + width
        Me.myFrame.Attributes("style") = style

        'If (ifHeight = 0 And ifWidth = 0) Then
        '    Me.myFrame.Attributes("onLoad") = "autoResize('myFrame')"
        'End If

        If (Not Scroll = "") Then
            Me.myFrame.Attributes("Scrolling") = Scroll
        End If

    End Sub

    Protected Sub rptItemListOnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rptItemList.ItemDataBound

        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then

            Dim lnkShowItem As LinkButton = e.Item.FindControl("lnkShowItem")
            Dim lnkEdit As HyperLink = e.Item.FindControl("lnkEdit")
            Dim lnkDelete As LinkButton = e.Item.FindControl("lnkDelete")
            Dim pnlAdmin As Panel = e.Item.FindControl("pnlAdmin")
            Dim t As Item = e.Item.DataItem

            lnkShowItem.CommandArgument = t.ItemId.ToString()

            If IsEditable And lnkDelete IsNot Nothing And lnkEdit IsNot Nothing Then
                pnlAdmin.Visible = True
                lnkDelete.CommandArgument = t.ItemId.ToString()
                lnkDelete.Enabled = True
                lnkDelete.Visible = True
                lnkEdit.Enabled = True
                lnkEdit.Visible = True
                lnkEdit.NavigateUrl = EditUrl(String.Empty, String.Empty, "Edit", "tid=" + t.ItemId.ToString())
                ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile))
            Else

                pnlAdmin.Visible = False

            End If

        End If

    End Sub

    Protected Sub rptItemListOnItemCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs) Handles rptItemList.ItemCommand

        Select Case e.CommandName

            Case "Delete"

                Dim tc As New ItemController
                tc.DeleteItem(e.CommandArgument, ModuleId)

            Case "ShowItem"

                Dim lnkShowItem As LinkButton = e.Item.FindControl("lnkShowItem")
                Dim URL As String = DotNetNuke.Common.Globals.NavigateURL() + "?tid=" + e.CommandArgument

                Response.Redirect(URL)

        End Select

        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL())

    End Sub
End Class